package com.mflex.mwms.activity;

import static com.xuexiang.xutil.XUtil.getContext;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.text.InputType;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

import com.mflex.mwms.BuildConfig;
import com.mflex.mwms.R;
import com.mflex.mwms.adapter.CommonDecoration;
import com.mflex.mwms.adapter.RecyclerViewAdapter;
import com.mflex.mwms.listener.OnRecyclerItemClickListener;
import com.mflex.mwms.model.MenuBean;
import com.mflex.mwms.utils.MMKVUtils;
import com.mflex.mwms.utils.MessageUtils;
import com.mflex.mwms.utils.XToastUtils;
import com.xuexiang.xui.widget.picker.widget.OptionsPickerView;
import com.xuexiang.xui.widget.picker.widget.builder.OptionsPickerBuilder;
import com.xuexiang.xui.widget.picker.widget.listener.OnOptionsSelectListener;
import com.xuexiang.xutil.common.StringUtils;


import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends BaseActivity implements View.OnTouchListener {

    private RecyclerView recyclerView;
    private RecyclerView recyclerView2;
    private RecyclerView recyclerView3;
    private RecyclerViewAdapter adapter;
    private RecyclerViewAdapter adapter2;
    private RecyclerViewAdapter adapter3;
    private List<MenuBean> listDatas = new ArrayList<MenuBean>();
    private List<MenuBean> listDatas2 = new ArrayList<MenuBean>();
    private List<MenuBean> listDatas3 = new ArrayList<MenuBean>();

    @BindView(R.id.stv_Warehose)
    TextView stvWarehose;

    private String[] warehoseOption;
    private int warehoseSelectOption = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        Intent intent = getIntent();
        ArrayList list = intent.getStringArrayListExtra("Warehoses");
        warehoseOption = new String[list.size()];
        list.toArray(warehoseOption);
        if (!StringUtils.isEmpty(MMKVUtils.getString("Warehose", ""))) {
            stvWarehose.setText(MMKVUtils.getString("Warehose", ""));
        }
        stvWarehose.setInputType(InputType.TYPE_NULL);
        init();
//        stvWarehose.setText("所属仓库：" + MMKVUtils.get("Warehose", ""));
        stvWarehose.setOnTouchListener(this);
    }

    private void init() {
        recyclerView = (RecyclerView) findViewById(R.id.main_recycleview);
        recyclerView2 = (RecyclerView) findViewById(R.id.main_recycleview2);
        recyclerView3 = (RecyclerView) findViewById(R.id.main_recycleview3);
        loadMenuData();

        recyclerView.setLayoutManager(new GridLayoutManager(this, 2));
        recyclerView.addItemDecoration(new CommonDecoration(this));

        recyclerView2.setLayoutManager(new GridLayoutManager(this, 2));
        recyclerView2.addItemDecoration(new CommonDecoration(this));

        recyclerView3.setLayoutManager(new GridLayoutManager(this, 2));
        recyclerView3.addItemDecoration(new CommonDecoration(this));

        adapter = new RecyclerViewAdapter(this, listDatas);
        adapter2 = new RecyclerViewAdapter(this, listDatas2);
        adapter3 = new RecyclerViewAdapter(this, listDatas3);
        recyclerView.setAdapter(adapter);
        recyclerView2.setAdapter(adapter2);
        recyclerView3.setAdapter(adapter3);

        adapter.setOnItemClickListener(new OnRecyclerItemClickListener() {
            @Override
            public void onRecyclerItemClick(RecyclerView parent, View view, int position) {
                if (StringUtils.isEmpty(stvWarehose.getText().toString())) {
                    XToastUtils.error("当前仓库未选择", 2000);
                    return;
                }
//                Intent intent = new Intent(MainActivity.this, PackWareHouseActivity.class);
                Intent intent = new Intent(MainActivity.this, listDatas.get(position).getTargertActivty());
//                intent.putExtra("userName", userName);
                startActivity(intent);
            }
        });

        adapter2.setOnItemClickListener(new OnRecyclerItemClickListener() {
            @Override
            public void onRecyclerItemClick(RecyclerView parent, View view, int position) {
                if (StringUtils.isEmpty(stvWarehose.getText().toString())) {
//                    XToastUtils.error("当前仓库未选择", 2000);
                    MessageUtils.errorMsgAndVoice(MainActivity.this, "当前仓库未选择");
                    return;
                }
                Intent intent = new Intent(MainActivity.this, listDatas2.get(position).getTargertActivty());
//                intent.putExtra("userName", userName);
                startActivity(intent);
            }
        });

        adapter3.setOnItemClickListener(new OnRecyclerItemClickListener() {
            @Override
            public void onRecyclerItemClick(RecyclerView parent, View view, int position) {
                if (StringUtils.isEmpty(stvWarehose.getText().toString())) {
//                    XToastUtils.error("当前仓库未选择", 2000);
                    MessageUtils.errorMsgAndVoice(MainActivity.this, "当前仓库未选择");
                    return;
                }
                Intent intent = new Intent(MainActivity.this, listDatas3.get(position).getTargertActivty());
//                intent.putExtra("userName", userName);
                startActivity(intent);
            }
        });
    }

    private void loadMenuData() {


        String channelName = BuildConfig.FLAVOR;
        //装箱入库
        listDatas.add(new MenuBean("入库上架", "", R.drawable.icon_more_operation_share_chat, PackWareHouseActivity.class));
//        listDatas.add(new MenuBean("拆箱", "", R.drawable.icon_more_operation_share_chat, UnBoxingActivity.class));
//        listDatas.add(new MenuBean("JADE防混箱比对", "", R.drawable.icon_more_operation_share_chat, UnBoxingActivity.class));
        listDatas.add(new MenuBean("华为比对", "", R.drawable.icon_more_operation_share_chat, HuaweiCompareActivity.class));
        listDatas.add(new MenuBean("零数箱二次比对", "", R.drawable.icon_more_operation_share_chat, UnFullCompareActivity.class));
//        listDatas.add(new MenuBean("华为内包拣货", "", R.drawable.icon_more_operation_share_chat, UnBoxingActivity.class));
//        listDatas.add(new MenuBeabangn("QA内包比对", "", R.drawable.icon_more_operation_share_chat, UnBoxingActivity.class));
        listDatas.add(new MenuBean("捷豹路虎标签打印", "", R.drawable.icon_more_operation_share_chat, JlrLabelPrintActivity.class));
        if (channelName.startsWith("YC")) {
            listDatas.add(new MenuBean("华为SN比对", "", R.drawable.icon_more_operation_share_chat, HuaweiSnCompareActivity.class));
            listDatas.add(new MenuBean("荣耀SN比对", "", R.drawable.icon_more_operation_share_chat, HonorSnCompActivity.class));
            listDatas.add(new MenuBean("满箱二次比对", "", R.drawable.icon_more_operation_share_chat, FullCompareActivity.class));
        }

        //库位管理
//        listDatas2.add(new MenuBean("调拨", "", R.drawable.icon_more_operation_share_chat, TransferOffMenuActivity.class));
        listDatas2.add(new MenuBean("调拨出库", "", R.drawable.icon_more_operation_share_chat, TransferOffActivity.class));
        listDatas2.add(new MenuBean("调拨接收", "", R.drawable.icon_more_operation_share_chat, TransferOnActivity.class));
        if (channelName.startsWith("YC")) {
            listDatas2.add(new MenuBean("移库", "", R.drawable.icon_more_operation_share_chat, MoveOnShelfOnActivity.class));
        }
        listDatas2.add(new MenuBean("移库任务", "", R.drawable.icon_more_operation_share_chat, MoveOnShelfOnTaskActivity.class));
        listDatas2.add(new MenuBean("仓库下架", "", R.drawable.icon_more_operation_share_chat, WarehouseShelfOffActivity.class));
//        listDatas2.add(new MenuBean("库存调整", "", R.drawable.icon_more_operation_share_chat, UnBoxingActivity.class));
//        listDatas2.add(new MenuBean("OBA抽检下架", "", R.drawable.icon_more_operation_share_chat, OBACheckShelfOffActivity.class));
//        listDatas2.add(new MenuBean("OBA归还上架", "", R.drawable.icon_more_operation_share_chat, OBAReturnShelfOnActivity.class));
//        listDatas2.add(new MenuBean("报废下架", "", R.drawable.icon_more_operation_share_chat, BPMScrapShelfOffActivity.class));
//        listDatas2.add(new MenuBean("工程领料下架", "", R.drawable.icon_more_operation_share_chat, BPMPickShelfOffActivity.class));
//        listDatas2.add(new MenuBean("RMA下架", "", R.drawable.icon_more_operation_share_chat, RMAShelfOffActivity.class));
        listDatas2.add(new MenuBean("盘点", "", R.drawable.icon_more_operation_share_chat, StockTaskListActivity.class));
        listDatas2.add(new MenuBean("新建移库任务", "", R.drawable.icon_more_operation_share_chat, MoveTaskActivity.class));

        //出货发运
        listDatas3.add(new MenuBean("拣货下架", "", R.drawable.icon_more_operation_share_chat, ShipmentOffActivity.class));
//        listDatas3.add(new MenuBean("华为荣耀出货比对", "", R.drawable.icon_more_operation_share_chat, UnBoxingActivity.class));
        listDatas3.add(new MenuBean("出货二次确认", "", R.drawable.icon_more_operation_share_chat, ShipmentCompareActivity.class));
//        if(channelName.startsWith("YC")){
//        listDatas3.add(new MenuBean("特斯拉客户标比对", "", R.drawable.icon_more_operation_share_chat, TeslaComapreActivity.class));
//        listDatas3.add(new MenuBean("QONE客户标比对", "", R.drawable.icon_more_operation_share_chat, QoneCompareActivity.class));
//        listDatas3.add(new MenuBean("西典客户标比对", "", R.drawable.icon_more_operation_share_chat, CatlXiDianCompareActivity.class));
//        listDatas3.add(new MenuBean("瑞庭客户标比对", "", R.drawable.icon_more_operation_share_chat, CatlRuiTingCompareActivity.class));
//        listDatas3.add(new MenuBean("捷豹路虎客户标比对", "", R.drawable.icon_more_operation_share_chat, JlrCompareActivity.class));
//        listDatas3.add(new MenuBean("小鹏客户标比对", "", R.drawable.icon_more_operation_share_chat, CcsCompareActivity.class));
        listDatas3.add(new MenuBean("图达通客户标比对", "", R.drawable.icon_more_operation_share_chat, TudaTongCompareActivity.class));
//        }
//        listDatas3.add(new MenuBean("箱号和栈板关联", "", R.drawable.icon_more_operation_share_chat, UnBoxingActivity.class));

    }

    @Override
    public void onClick(View view) {

    }

    @Override
    public boolean onKey(View view, int i, KeyEvent keyEvent) {
        return false;
    }

    /**
     * 所属仓库选择
     */
    private void showWarehosePickerView() {
        OptionsPickerView pvOptions = new OptionsPickerBuilder(MainActivity.this, new OnOptionsSelectListener() {
            @Override
            public boolean onOptionsSelect(View v, int options1, int options2, int options3) {
                warehoseSelectOption = options1;
                stvWarehose.setText(warehoseOption[warehoseSelectOption]);
                MMKVUtils.put("Warehose", warehoseOption[warehoseSelectOption]);
                return false;
            }
        })
                .setTitleText("所属仓库")
                .setSelectOptions(warehoseSelectOption)
                .build();
        pvOptions.setPicker(warehoseOption);
        pvOptions.show();
    }

    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        switch (motionEvent.getAction()) {
            case MotionEvent.ACTION_DOWN:
                switch (view.getId()) {
                    case R.id.stv_Warehose:
                        showWarehosePickerView();
                        break;
                    default:
                        break;
                }
                break;
            default:
                break;
        }

        return false;
    }
}